package com.jwthhealth_pub.old.individual.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jwthhealth_pub.R;

/**
 * 九种体制柱状图
 * Created by liuzhen on 2017/12/4.
 */

public class EvaluateChart extends View {

    private String[] xValues;
    private int[] yValues;

    public void setAxisValues(String[] xValues, int[] yValue) {
        this.xValues = xValues;
        this.yValues = yValue;
        invalidate();
    }

    public EvaluateChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int mHei = getHeight();
        Log.d("EvaluateChart", "getHeight():" + mHei);
        int mWid = getWidth();
        Log.d("EvaluateChart", "getWidth():" + mWid);

        int chartWdi = mWid - 75;
        int yValueHei = mHei - 150;
        int yAxisHei = mHei - 135;

        Paint linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(3);
        linePaint.setColor(getResources().getColor(R.color.primarycolor));
        canvas.drawLine(75, yAxisHei, chartWdi, yAxisHei, linePaint);//x axis
        canvas.drawLine(75, 0, 75, yAxisHei, linePaint);//y axis

        Paint textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setAntiAlias(true);
        int textSize = 35;
        textPaint.setTextSize(textSize);
        linePaint.setStrokeWidth(1);
        linePaint.setColor(getResources().getColor(R.color.dividual_gray));
        canvas.drawText("0", 0, (yValueHei+15), textPaint);
//        canvas.drawLine(75, (yValueHei+15), chartWdi, (yValueHei+15), linePaint);
        canvas.drawText("30", 0, (float) ((yValueHei+15) * .7), textPaint);
        canvas.drawLine(75, (float) ((yValueHei+15) * .7), chartWdi, (float) ((yValueHei+15) * .7), linePaint);
        canvas.drawText("40", 0, (float) ((yValueHei+15) * .6), textPaint);
        canvas.drawLine(75, (float) ((yValueHei+15) * .6), chartWdi, (float) ((yValueHei+15) * .6), linePaint);
        canvas.drawText("60", 0, (float) ((yValueHei+15) * .4), textPaint);
        canvas.drawLine(75, (float) ((yValueHei+15) * .4), chartWdi, (float) ((yValueHei+15) * .4), linePaint);
        canvas.drawText("100", 0, 35, textPaint);
        canvas.drawLine(75, 15, chartWdi, 15, linePaint);


        int xValueSpace = mWid / 12;

        if (xValues == null || xValues.length != 9) return;

        for (int i = 0; i < xValues.length; i++) {
//            canvas.drawText(xValues[i], xValueSpace * 2 + xValueSpace * i, chartHei+50, textPaint);

            char[] chars = xValues[i].toCharArray();
            for (int j=0; j<chars.length; j++) {
                canvas.drawText(String.valueOf(chars[j]), (float) (xValueSpace * (i + 2) - textSize/2), yAxisHei+(textSize+5)*(j+1), textPaint);
            }
        }

        if (yValues == null || yValues.length != 9) return;

        Paint rectPaint = new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setColor(getResources().getColor(R.color.primarycolor));
        for (int i = 0; i < yValues.length; i++) {
            int y = yValueHei * (1 - yValues[i] / 100);
            if (y == yValueHei)
                y = yValueHei - 5;//缺省值

            canvas.drawRect(xValueSpace * (i + 2) - 25, y + 15, //顶部留有15的间隙
                    xValueSpace * (i + 2) + 25, yValueHei + 15, rectPaint);
            Log.d("EvaluateChart", "i:" + i);
            Log.d("EvaluateChart", "y:" + y);
            Log.d("EvaluateChart", "chartHei:" + yValueHei);
        }
    }
}
